home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 3 / ct-rom iiib.zip / ct-rom iiib / WINDOWS / GRAFISCH / FRACXTR5 / FRACZOOM.ZIP / FRACZOOM.C < prev    next >
C/C++ Source or Header  |  1992-03-22  |  7KB  |  269 lines

  1. /*
  2.  *  FraZoom.c    (c) Arto V. Viitanen 1992
  3.  *
  4.  *    Based on the idea of the Fractfly program by Josh Lannin.
  5.  *    Version 1.0. 3/22/92.
  6.  */
  7.  
  8. #ifdef __MSDOS__
  9. #include <stdlib.h>
  10. #include <alloc.h>
  11. #include <conio.h>
  12. #include <io.h>
  13. #endif                           /* __MSDOS__ */
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <fcntl.h>
  18. #include <assert.h>
  19. #include <math.h>
  20. #include "gif_lib.h"
  21. #include "getarg.h"
  22. #include "fractinf.h"
  23.  
  24. #define PROGRAM_NAME    "FracZoom"
  25. #ifdef __MSDOS__
  26. extern unsigned int
  27.     _stklen = 16384;               /* Increase default stack
  28.                         * size. */
  29. #endif                           /* __MSDOS__ */
  30. #ifdef SYSV
  31. static char
  32.     *VersionStr = "FracZoom,\t\tArto V. Viitanen\n\
  33.     (C) Copyright 1992 Arto V. Viitanen, Non commercial use only.\n";
  34.  
  35. static char
  36.        *CtrlStr = "FracZoom GifFile%*s";
  37. #else
  38. static char
  39.     *VersionStr =
  40.     PROGRAM_NAME
  41.     GIF_LIB_VERSION
  42.     "    Arto V. Viitanen, "
  43.     __DATE__ ",   " __TIME__ "\n"
  44.     "(C) Copyright 1992, Arto V. Viitanen Non commercial use only.\n";
  45. static char
  46.        *CtrlStr =
  47. PROGRAM_NAME
  48. " GifFile!*s";
  49. #endif                           /* SYSV */
  50.  
  51.  
  52.  
  53. static void ReadInfo(char* FileName, struct fractal_info* info);
  54. static void GenerateFly(struct fractal_info* start, struct fractal_info* end,
  55.             char* start_file, char* end_file);
  56. static int  CheckFract(int ExtCode, GifByteType * Extension);
  57. static void GetFileName(char *FileType, char** FileName);
  58.  
  59.  
  60.  
  61. int main(int argc, char **argv) {
  62.  
  63.     int    Error, NumFiles;
  64.     char  *StartGifFileName, *EndGifFileName, **FileName = NULL;
  65.     struct fractal_info start_info, end_info;
  66.  
  67.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  68.                &NumFiles, &FileName)) != FALSE ||
  69.         (NumFiles > 2)) {
  70.     fprintf(stderr,"%s\n", VersionStr);
  71.     if (Error)
  72.         GAPrintErrMsg(Error);
  73.     else if (NumFiles > 2)
  74.         GIF_MESSAGE("Error in command line parsing - two GIF files please.");
  75.     GAPrintHowTo(CtrlStr);
  76.     return 1;
  77.     }
  78.  
  79.     switch (NumFiles) {
  80.     case 0:
  81.         GetFileName("Start", &StartGifFileName);
  82.         GetFileName("End", &EndGifFileName);
  83.         break;
  84.     case 1:
  85.         StartGifFileName = FileName[0];
  86.         GetFileName("End", &EndGifFileName);
  87.         break;
  88.     default:
  89.         StartGifFileName = FileName[0];
  90.         EndGifFileName = FileName[1];
  91.     }
  92.  
  93.     ReadInfo(StartGifFileName, &start_info);
  94.     ReadInfo(EndGifFileName, &end_info);
  95.  
  96.     GenerateFly(&start_info, &end_info, StartGifFileName, EndGifFileName);
  97.     free(StartGifFileName);
  98.     free(EndGifFileName);
  99.     return 0;
  100.  
  101. }
  102.  
  103. static void GetFileName(char *FileType, char** FileName) {
  104.  
  105.     char buffer[120];
  106.  
  107.     printf("Give name of %s GIF file ", FileType);
  108.     fflush(stdout);
  109.     gets(buffer);
  110.  
  111.     *FileName = malloc(strlen(buffer)+1);
  112.     strcpy(*FileName, buffer);
  113.  
  114. }
  115.  
  116. static void ReadInfo(char* FileName, struct fractal_info* info) {
  117.  
  118.     GifRecordType RecordType;
  119.     GifByteType *CodeBlock, *Extension;
  120.     GifFileType *GifFile;
  121.     char   *p;
  122.     int CodeSize, ExtCode, Len;
  123.  
  124.  
  125.     if ((GifFile = DGifOpenFileName(FileName)) == NULL) {
  126.         PrintGifError();
  127.         exit(-1);
  128.     }
  129.  
  130.  
  131.     printf("\n%s:\n\tScreen Size - Width = %d, Height = %d.\n",
  132.        FileName, GifFile->SWidth, GifFile->SHeight);
  133.  
  134.     do {
  135.     if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) {
  136.         PrintGifError();
  137.         exit(-1);
  138.     }
  139.     switch (RecordType) {
  140.         case IMAGE_DESC_RECORD_TYPE:
  141.         if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
  142.             PrintGifError();
  143.             exit(-1);
  144.         }
  145.         /* Skip the image: */
  146.         if (DGifGetCode(GifFile, &CodeSize, &CodeBlock) == GIF_ERROR) {
  147.             PrintGifError();
  148.             exit(-1);
  149.         }
  150.         while (CodeBlock != NULL) {
  151.             if (DGifGetCodeNext(GifFile, &CodeBlock) == GIF_ERROR) {
  152.             PrintGifError();
  153.             exit(-1);
  154.             }
  155.         }
  156.         break;
  157.         case EXTENSION_RECORD_TYPE:
  158.         if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
  159.             PrintGifError();
  160.             exit(-1);
  161.         }
  162.         p = (char*) info; /* Collect the extension data */
  163.  
  164.         if (CheckFract(ExtCode, Extension)) {
  165.             while (Extension != NULL) {
  166.             if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
  167.                 PrintGifError();
  168.                 exit(1);
  169.             }
  170.             Len = Extension[0];
  171.             if (Len == 0 || Extension == NULL)
  172.                 break;
  173.             memcpy(p, &Extension[1], Len);
  174.             p += Len;
  175.             }
  176.         }
  177.         else {
  178.             fprintf(stderr,"\007%s is not fractint picture\n",
  179.                 FileName);
  180.             exit(2);
  181.         }
  182.         break;
  183.         default:
  184.         break;
  185.     }
  186.     } while (RecordType != TERMINATE_RECORD_TYPE);
  187.  
  188.  
  189.     if (DGifCloseFile(GifFile) == GIF_ERROR) {
  190.     PrintGifError();
  191.     exit(-1);
  192.     }
  193. }
  194.  
  195. static int CheckFract(int ExtCode, GifByteType * Extension) {
  196.  
  197.     int     Len;
  198.  
  199.     if (ExtCode != 255)
  200.     return 0;
  201.     Len = Extension[0];
  202.     if (Len != 11)
  203.     return 0;                   /* not fractint gif */
  204.     if (strncmp(&Extension[1], "fractint001", 11) != 0)
  205.     return 0;
  206.  
  207.     printf("This is fractint file\n");
  208.     return 1;
  209. }
  210.  
  211. static void GenerateFly(struct fractal_info *start, struct fractal_info *end,
  212.             char* start_file, char* end_file) {
  213.  
  214.     char buffer[80];
  215.     int steps, i;
  216.     double decx1, decx2, decy1, decy2;
  217.     double x1, x2, y1, y2;
  218.     FILE* out;
  219.  
  220.     if (start->fractal_type != end->fractal_type) {
  221.     fprintf(stderr,"\007Files have different kind of fractals.\n");
  222.     fprintf(stderr,"%s has %s but %s has %s\n",
  223.         start_file, name_of[start->fractal_type],
  224.         end_file, name_of[end->fractal_type]);
  225.     exit(1);
  226.     }
  227.     else
  228.     printf("Both are %s\n", name_of[end->fractal_type]);
  229.  
  230.     do {
  231.     printf("How many steps ");fflush(stdout);
  232.     gets(buffer);
  233.     steps=atoi(buffer);
  234.     if (!steps)
  235.         fprintf(stderr,"%s is not positive number\n",buffer);
  236.     } while (!steps);
  237.  
  238.     do {
  239.     printf("Give batchfile name ");fflush(stdout);
  240.     gets(buffer);
  241.  
  242.     if ((out = fopen(buffer, "wt")) == NULL)
  243.         fprintf(stderr,"Cannot create %s\n", buffer);
  244.     } while (!out);
  245.  
  246.  
  247.     x1 = start->xmin;
  248.     x2 = start->xmax;
  249.     y1 = start->ymin;
  250.     y2 = start->ymax;
  251.     decx1 = ( (x1 - end->xmin) / steps);
  252.     decx2 = ( (x2 - end->xmax) / steps);
  253.     decy1 = ( (y1 - end->ymin) / steps);
  254.     decy2 = ( (y2 - end->ymax) / steps);
  255.  
  256.     for (i=steps; i>0; i--) {
  257.     fprintf(out,"Fractint batch=yes type=%s corners=",
  258.         name_of[start->fractal_type]);
  259.     fprintf(out, "%10.10f/%10.10f/%10.10f/%10.10f\n",
  260.         x1, x2, y1, y2);
  261.  
  262.     x1 -= decx1;
  263.     x2 -= decx2;
  264.     y1 -= decy1;
  265.     y2 -= decy2;
  266.     }
  267.     fclose(out);
  268. }
  269.